package com.itheima.common.shiro.entity;

import com.itheima.common.utils.PermissionConstants;
import lombok.*;
import org.crazycake.shiro.AuthCachePrincipal;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.*;

/**
 * @Author IntelliJ IDEA
 * @Date 2020/11/2 21:32
 * @Version 1.0
 */
@NoArgsConstructor
@Setter
@Getter
@EqualsAndHashCode
@ToString
public class UserInfo implements Serializable, AuthCachePrincipal {

    /**
     * 手机号
     */
    private String mobile;

    /**
     * 用户名
     */
    private String username;

    /**
     * 企业id
     */
    private String companyId;

    /**
     * 企业名称
     */
    private String companyName;

    /**
     * 用户id
     */
    private String userId;

    private Map<String,Object> roles=new HashMap<String,Object>();


    public UserInfo(User user){
        Set<String> menu=new HashSet<String>(0);
        Set<String> apis=new HashSet<String>(0);
        Set<String> points=new HashSet<String>(0);
        if(user.getRole()!=null){
            for (Role role : user.getRole()) {
                if(role.getPermissions()!=null){
                    for (Permission permission : role.getPermissions()) {
                        if(permission.getType()== PermissionConstants.PERMISSION_MENU){
                            menu.add(permission.getCode());
                        }else if(permission.getType()==PermissionConstants.PERMISSION_POINT){
                            points.add(permission.getCode());
                        }else {
                            apis.add(permission.getCode());
                        }
                    }
                }
            }
        }
        Map<String,Object> map=new HashMap<String,Object>();
        this.mobile=user.getMobile();
        this.username=user.getUsername();
        this.companyId=user.getCompanyId();
        this.companyName=user.getCompanyName();
        this.userId=user.getId();
        map.put("menus",menu);
        map.put("points",points);
        map.put("apis",apis);

    }


    public UserInfo(User user, List<Permission> permissionList){
        Set<String> menu=new HashSet<String>(0);
        Set<String> apis=new HashSet<String>(0);
        Set<String> points=new HashSet<String>(0);
        if(!CollectionUtils.isEmpty(permissionList)){
            for (Permission permission : permissionList) {
                if(permission.getType()==1){
                    menu.add(permission.getCode());
                }else if(permission.getType()==2){
                    apis.add(permission.getCode());
                }else {
                    points.add(permission.getCode());
                }
            }
        }
        this.roles.put("menus",menu);
        this.roles.put("points",points);
        this.roles.put("apis",apis);
        this.mobile=user.getMobile();
        this.username=user.getUsername();
        this.companyId=user.getCompanyId();
        this.companyName=user.getCompanyName();
        this.userId=user.getId();
    }

    @Override
    public String getAuthCacheKey() {
        return null;
    }
}
